package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Goods;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IGoodsService;
import com.woniu.supermarket.service.IImgsService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("goods")
@Api(value = "商品操作")
public class GoodsController {

    @Autowired
    IGoodsService IGoodsService;

    @Autowired
    IImgsService iImgsService;

    @GetMapping("/showGoods")
    @ApiOperation(value = "商品分页查询操作")
    public ResultResponse<PageInfo<GoodsVo>> showGoods(@ApiParam(name = "now",value = "当前页") @RequestParam(name = "now",defaultValue = "1")Integer now,
                                                   @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3")Integer size,
                                                   String name,
                                                   @ApiParam(name = "typrId",value = "接收前台传来的商品类别id")@RequestParam(name = "typeId",defaultValue = "0")Integer typeId){
        System.out.println(name);
        ResultResponse<PageInfo<GoodsVo>> result = null;
        try {
            PageInfo<GoodsVo> pageInfo = IGoodsService.findGoodsByCons(now, size, name, typeId);
            result = new ResultResponse<>(200,"查询商品成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询商品失败");
        }
        return result;
    }

    @PostMapping("/addGoods")
    @ApiOperation(value = "添加商品")
    public ResultResponse<Void> addGoods(@ApiParam(name = "goods",value = "接收商品信息")@RequestBody Goods goods){
        ResultResponse<Void> result = null;
        try {
            int i = IGoodsService.addGoods(goods);

            result = new ResultResponse<>(200,"添加商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加商品失败");
        }
        return result;
    }

    @PutMapping("/modifyGoods")
    @ApiOperation(value = "修改商品")
    public ResultResponse<Void> modify(@RequestBody Goods goods){
        ResultResponse<Void> result = null;
        try {
            int i = IGoodsService.modifyGoods(goods);
            result = new ResultResponse<>(200,"修改商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改商品失败");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除商品")
    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;
        try {
            int i = IGoodsService.removeGoods(id);
            result = new ResultResponse<>(200,"删除商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除商品失败");
        }
        return result;
    }

    @DeleteMapping("/delBatch")
    @ApiOperation(value = "批量删除商品")
    public ResultResponse<Void> delBatch(@RequestParam("ids") int[] ids){
        ResultResponse<Void> result = null;
        try{
            int i = IGoodsService.removeBatchGoods(ids);
            //删除图片库关联
            iImgsService.removeBatchIGDVByIds(ids);

            result = new ResultResponse<>(200,"批量删除商品成功");

        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"批量删除商品失败");
        }
        return result;
    }

    @GetMapping("/showCode")
    @ApiOperation("添加时，先生成商品编码显示在页面")
    public ResultResponse<String> code(){
        ResultResponse<String> result = null;

        try {
            String code = CodeCreateUtil.createGoodsCode();
            result = new ResultResponse<>(200,"查询编码成功",code);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询编码失败");
        }
       return result;
    }

    @GetMapping("/findByCode/{code}")
    @ApiOperation(value = "通过商品编码查询商品id")
    public ResultResponse<Integer> findByCode(@PathVariable("code") String code){
        ResultResponse<Integer> result = null;
        try {
            int gid = IGoodsService.findGidByCode(code);
            result = new ResultResponse<Integer>(200,"查询商品id成功",gid);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<Integer>(201,"查询商品id失败");
        }
        return result;
    }


    @PostMapping("/addGoodImg")
    @ApiOperation(value = "添加商品")
    public ResultResponse<Void> addGoodImg(@ApiParam(name = "goodsVo",value = "接收商品信息")@RequestBody GoodsVo goodsvo){
        ResultResponse<Void> result = null;
        try {
            int i = IGoodsService.addGoodsVo(goodsvo);

            result = new ResultResponse<>(200,"添加商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加商品失败");
        }
        return result;
    }


    @PostMapping("/editGoodImg")
    @ApiOperation(value = "添加商品")
    public ResultResponse<Void> editGoodImg(@ApiParam(name = "goodsVo",value = "接收商品信息")@RequestBody GoodsVo goodsvo){
        ResultResponse<Void> result = null;
        try {
            int i = IGoodsService.editGoodsVo(goodsvo);

            result = new ResultResponse<>(200,"添加商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加商品失败");
        }
        return result;
    }


    @GetMapping("/showGood")
    @ApiOperation(value = "根据仓库查询商品操作")
    public ResultResponse<PageInfo<GoodsVo>> showGood(@ApiParam(name = "now",value = "当前页") @RequestParam(name = "now",defaultValue = "1")Integer now,
                                                       @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3")Integer size){
        ResultResponse<PageInfo<GoodsVo>> result = null;
        try {
            result = new ResultResponse<>(200,"查询商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询商品失败");
        }
        return result;
    }


}
